<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isShowDialog=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="deleteRole(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 弹窗的占位 -->
      <el-dialog :title="title" :visible="isShowDialog" @close="btnCancel">
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button
              size="small"
              type="primary"
              @click="btnOK"
            >确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getCompanyInfo,
  getRoleList,
  deleteRole,
  getRoleDetail,
  updateRole,
  addRole
} from '@/api/setting'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      // 角色列表
      list: [],
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      // 角色表单的数据对象
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { min: 3, max: 50, message: '长度要在 3-50 之间' }
        ],
        description: [
          { required: true, message: '数据不能为空', trigger: 'blur' },
          { min: 3, max: 200, message: '长度要在 3-12 之间' }
        ]
      },
      isShowDialog: false,

      // 公司数据表单的固定数据对象
      formData: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      }
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    title() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.getCompanyInfo()
    this.getRoleList()
  },
  methods: {
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
    },
    currentChange(newPage) {
      // 1. 将用户选择的页码覆盖默认选项
      this.page.page = newPage
      this.getRoleList()
    },
    async deleteRole(id) {
      try {
        // 发起请求删除角色
        // 角色删除需要 id
        console.log(id)
        // 重点是发请求
        // 1. 二次确认
        await this.$confirm('是否确认要删除该角色吗?')
        // 2. 发请求
        await deleteRole(id)
        // 3. 提醒用户成功
        this.$message.success('删除角色成功')
        // 4. 重新加载数据
        // 这里可以做一个优化
        // 如果当前页面只剩下最后一条数据, 而且当前页不是第一页
        if (this.list.length === 1 && this.page.page !== 1) {
          this.page.page--
        }

        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    async editRole(id) {
      // 1. 获取旧数据回显
      this.roleForm = await getRoleDetail(id)
      // 2. 弹出弹窗
      this.isShowDialog = true
    },
    btnCancel() {
      // 0. 将整个表单数据变回原始状态,(所有回显的数据都被抛弃)
      this.roleForm = {
        name: '',
        description: ''
      }
      // 1. 清理表单数据和错误
      this.$refs.roleForm.resetFields()
      // 2. 关闭弹窗
      this.isShowDialog = false
    },
    async btnOK() {
      try {
        // 1. 发送前校验
        await this.$refs.roleForm.validate()
        // 2. 发送
        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm)
        } else {
          // 新增
          await addRole(this.roleForm)
        }
        // 3. 通知用户发送完毕
        this.$message.success('操作成功')
        // 4. 更新页面
        this.getRoleList()
        // 5. 关闭弹窗
        this.isShowDialog = false
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>
</style>
